<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- calculatorbuilder.qdoc -->
  <title>计算器生成器示例 | Qt设计师手册</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtdesigner-manual.html">Qt设计师手册</a></td><td ><a href="examples-designer.html">Qt<span lang="zh-cn">设计师示例</span></a></td><td >
			计算器生成器示例</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtdesigner-manual.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtdesigner/qtdesigner-calculatorbuilder-example.html#preparation"><font style="vertical-align: inherit;">准备</font></a></li>
<li class="level1"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtdesigner/qtdesigner-calculatorbuilder-example.html#calculatorform-class-definition"><font style="vertical-align: inherit;">CalculatorForm 类定义</font></a></li>
<li class="level1"><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtdesigner/qtdesigner-calculatorbuilder-example.html#calculatorform-class-implementation"><font style="vertical-align: inherit;">CalculatorForm 类实现</font></a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">计算器生成器示例</h1>
<span class="subtitle"></span>
<!-- $$$calculatorbuilder-brief -->
<p><font style="vertical-align: inherit;">在运行时从</font><i><font style="vertical-align: inherit;">Qt Designer</font></i><font style="vertical-align: inherit;">表单创建用户界面。</font></p>
<!-- @@@calculatorbuilder -->
<!-- $$$calculatorbuilder-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/calculatorbuilder-example.png" alt="" /></p><p><font style="vertical-align: inherit;">我们使用</font><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtdesigner/qtdesigner-calculatorform-example.html"><font style="vertical-align: inherit;">计算器表单</font></a><font style="vertical-align: inherit;">示例中创建的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtdesigner/qtdesigner-calculatorform-example.html">表单</a>来展示在构建应用程序时可以在执行或定义应用程序时生成相同的用户界面。</font></p>
<a name="preparation"></a>
<h2 id="preparation"><span lang="zh-cn">准备</span></h2>
<p><font style="vertical-align: inherit;">该</font><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtdesigner/qtdesigner-calculatorform-example.html"><font style="vertical-align: inherit;">计算器形式</font></a><font style="vertical-align: inherit;">示例定义，我们可以不加修改地使用的用户界面。在这个例子中，我们使用一个</font><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/resources.html"><font style="vertical-align: inherit;">资源文件</font></a><font style="vertical-align: inherit;">来包含</font><code>calculatorform.ui</code><font style="vertical-align: inherit;">在上一个例子中创建的文件，但它可以存储在磁盘上。</font></p>
<p><font style="vertical-align: inherit;">要在运行时生成表单，我们需要将示例链接到</font><code>QtUiTools</code><font style="vertical-align: inherit;">模块库。我们使用的项目文件包含执行此操作所需的所有信息：</font></p>
<pre class="cpp">

  HEADERS     = calculatorform.h
  RESOURCES   = calculatorbuilder.qrc
  SOURCES     = calculatorform.cpp \
                main.cpp
  QT += widgets uitools

</pre>
<p><font style="vertical-align: inherit;">所有其他必要的文件都像往常一样声明。</font></p>
<a name="calculatorform-class-definition"></a>
<h2 id="calculatorform-class-definition"><font style="vertical-align: inherit;">CalculatorForm 类定义</font></h2>
<p><font style="vertical-align: inherit;">在</font><code>CalculatorForm</code><font style="vertical-align: inherit;">类定义用于承载形式的用户界面窗口组件：</font></p>
<pre class="cpp">

  <span class="keyword">class</span> CalculatorForm : <span class="keyword">public</span> <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      <span class="keyword">explicit</span> CalculatorForm(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> nullptr);

  <span class="keyword">private</span> <span class="keyword">slots</span>:
      <span class="type">void</span> on_inputSpinBox1_valueChanged(<span class="type">int</span> value);
      <span class="type">void</span> on_inputSpinBox2_valueChanged(<span class="type">int</span> value);

  <span class="keyword">private</span>:
      <span class="type"><a href="../qtwidgets/qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>ui_inputSpinBox1;
      <span class="type"><a href="../qtwidgets/qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>ui_inputSpinBox2;
      <span class="type"><a href="../qtwidgets/qlabel.html">QLabel</a></span> <span class="operator">*</span>ui_outputWidget;
  };

</pre>
<p><font style="vertical-align: inherit;">请注意，我们不需要包含头文件来描述用户界面。我们只定义了两个公共槽，使用 所需的自动连接命名约定</font><code>uic</code><font style="vertical-align: inherit;">，并声明私有变量，我们将使用这些变量在构造后访问表单提供的组件。</font></p>
<a name="calculatorform-class-implementation"></a>
<h2 id="calculatorform-class-implementation"><font style="vertical-align: inherit;">CalculatorForm 类实现</font></h2>
<p><font style="vertical-align: inherit;">我们将需要使用库提供的</font><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtuitools/quiloader.html"><font style="vertical-align: inherit;">QUiLoader</font></a><font style="vertical-align: inherit;">类</font><code>libQtUiTools</code><font style="vertical-align: inherit;">，因此我们首先确保包含模块的头文件：</font></p>
<pre class="cpp">

  <span class="preprocessor">#include &lt;QtUiTools&gt;</span>

</pre>
<p><font style="vertical-align: inherit;">构造函数使用表单加载器对象来构造我们通过</font><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/qfile.html"><font style="vertical-align: inherit;">QFile</font></a><font style="vertical-align: inherit;">对象从示例资源中检索的用户界面：</font></p>
<pre class="cpp">

  CalculatorForm<span class="operator">::</span>CalculatorForm(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span>(parent)
  {
      <span class="type"><a href="../qtuitools/quiloader.html">QUiLoader</a></span> loader;

      <span class="type"><a href="../qtcore/qfile.html">QFile</a></span> file(<span class="string">&quot;:/forms/calculatorform.ui&quot;</span>);
      file<span class="operator">.</span>open(<span class="type"><a href="../qtcore/qfile.html">QFile</a></span><span class="operator">::</span>ReadOnly);
      <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>formWidget <span class="operator">=</span> loader<span class="operator">.</span>load(<span class="operator">&amp;</span>file<span class="operator">,</span> <span class="keyword">this</span>);
      file<span class="operator">.</span>close();

</pre>
<p><font style="vertical-align: inherit;">通过在示例资源中包含用户界面，我们确保在示例运行时它会出现。该</font><code>loader.load()</code><font style="vertical-align: inherit;">函数采用文件中包含的用户界面描述，并将表单组件构造为</font><code>CalculatorForm</code><font style="vertical-align: inherit;">.</font></p>
<p><font style="vertical-align: inherit;">我们对生成的用户界面中的三个组件感兴趣：两个旋转框和一个标签。为方便起见，我们从由 构造的组件中检索指向这些组件的指针</font><code>FormBuilder</code><font style="vertical-align: inherit;">，并记录它们以备后用。该</font><code>qFindChild()</code><font style="vertical-align: inherit;">模板的功能使我们能够查询控件，以便找到一个<span lang="zh-cn">命名的</span>子组件。</font></p>
<pre class="cpp">

      ui_inputSpinBox1 <span class="operator">=</span> findChild<span class="operator">&lt;</span><span class="type"><a href="../qtwidgets/qspinbox.html">QSpinBox</a></span><span class="operator">*</span><span class="operator">&gt;</span>(<span class="string">&quot;inputSpinBox1&quot;</span>);
      ui_inputSpinBox2 <span class="operator">=</span> findChild<span class="operator">&lt;</span><span class="type"><a href="../qtwidgets/qspinbox.html">QSpinBox</a></span><span class="operator">*</span><span class="operator">&gt;</span>(<span class="string">&quot;inputSpinBox2&quot;</span>);
      ui_outputWidget <span class="operator">=</span> findChild<span class="operator">&lt;</span><span class="type"><a href="../qtwidgets/qlabel.html">QLabel</a></span><span class="operator">*</span><span class="operator">&gt;</span>(<span class="string">&quot;outputWidget&quot;</span>);

</pre>
<p><font style="vertical-align: inherit;">表单加载器创建的组件需要连接到</font><code>CalculatorForm</code><font style="vertical-align: inherit;">对象中专门命名的插槽。我们使用 Qt 的元对象系统来启用这些连接：</font></p>
<pre class="cpp">

      <span class="type"><a href="../qtcore/qmetaobject.html">QMetaObject</a></span><span class="operator">::</span>connectSlotsByName(<span class="keyword">this</span>);

</pre>
<p><font style="vertical-align: inherit;">将表单组件添加到布局中，并设置窗口标题：</font></p>
<pre class="cpp">

      <span class="type"><a href="../qtwidgets/qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>layout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qvboxlayout.html">QVBoxLayout</a></span>;
      layout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(formWidget);
      setLayout(layout);

      setWindowTitle(tr(<span class="string">&quot;Calculator Builder&quot;</span>));
  }

</pre>
<p><font style="vertical-align: inherit;">表单提供的修改widget的两个slots的定义方式与</font><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtdesigner/qtdesigner-calculatorform-example.html"><font style="vertical-align: inherit;"><span lang="zh-cn">计算器表单</span></font></a><font style="vertical-align: inherit;">示例中的类似，除了我们从spin 
box中读取值并通过我们在构造函数中记录的指针将结果写入输出<span lang="zh-cn">组件</span> ：</font></p>
<pre class="cpp">

  <span class="type">void</span> CalculatorForm<span class="operator">::</span>on_inputSpinBox1_valueChanged(<span class="type">int</span> value)
  {
      ui_outputWidget<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(value <span class="operator">+</span> ui_inputSpinBox2<span class="operator">-</span><span class="operator">&gt;</span>value()));
  }

  <span class="type">void</span> CalculatorForm<span class="operator">::</span>on_inputSpinBox2_valueChanged(<span class="type">int</span> value)
  {
      ui_outputWidget<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(value <span class="operator">+</span> ui_inputSpinBox1<span class="operator">-</span><span class="operator">&gt;</span>value()));
  }

</pre>
<p><font style="vertical-align: inherit;">这种方法的优点是我们可以在应用程序运行时替换表单，但我们仍然可以操作它包含的组件，只要它们被赋予适当的名称。</font></p>
<p><span lang="zh-cn">文件：</span></p>
<ul>
<li><a href="qtdesigner-calculatorbuilder-calculatorbuilder-pro.html">calculatorbuilder/calculatorbuilder.pro</a></li>
<li><a href="qtdesigner-calculatorbuilder-calculatorbuilder-qrc.html">calculatorbuilder/calculatorbuilder.qrc</a></li>
<li><a href="qtdesigner-calculatorbuilder-calculatorform-cpp.html">calculatorbuilder/calculatorform.cpp</a></li>
<li><a href="qtdesigner-calculatorbuilder-calculatorform-h.html">calculatorbuilder/calculatorform.h</a></li>
<li><a href="qtdesigner-calculatorbuilder-calculatorform-ui.html">calculatorbuilder/calculatorform.ui</a></li>
<li><a href="qtdesigner-calculatorbuilder-main-cpp.html">calculatorbuilder/main.cpp</a></li>
</ul>
</div>
<!-- @@@calculatorbuilder -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
